package cd.safe.school.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import cd.safe.school.R;
import cd.safe.school.adapter.ImageMienAdapter;
import cd.safe.school.adapter.SchoolMienAdapter;
import cd.safe.school.bean.SchoolNew;
import cd.safe.school.util.Parse;
import cd.safe.school.view.MyGallery;

public class SchoolMienActivity extends Activity implements OnClickListener {
	private Timer timer = new Timer(true);
	private ListView schoolmien_list;
	private List<Map<String, Object>> ls = new ArrayList<Map<String, Object>>();
	private ProgressDialog pd;
	private List<SchoolNew> lst = new ArrayList<SchoolNew>();
	public static Handler handler;
	private MyGallery mGalleryItem;
	private ImageView gallery_item_01;
	private ImageView gallery_item_02;
	private ImageView gallery_item_03;

	private static final int timerAnimation = 1;
	private List<Bitmap> lists = new ArrayList<Bitmap>();

	private Handler mHandler = new Handler() {
		public void handleMessage(Message message) {

			super.handleMessage(message);

			switch (message.what) {

			case timerAnimation:

				int position = mGalleryItem.getSelectedItemPosition();

				if (position > 1) {

					mGalleryItem.setSelection(0);

				} else if (position < 1) {

					imageViewOutAniamtion(position);

					mGalleryItem.setSelection(position + 1);

				} else {

					imageViewOutAniamtion(position);

					mGalleryItem.setSelection(position + 1);

				}

				break;

			}

		}

	};

	public void imageViewOutAniamtion(int position) {

		ImageView img = (ImageView) mGalleryItem.getChildAt(position);

		img.startAnimation(AnimationUtils.loadAnimation(
				SchoolMienActivity.this,

				R.anim.gallery_out));
		

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.schoolmien_main);
		schoolmien_list = (ListView) findViewById(R.id.schoolmien_list);
		// 设置公告信息
		gallery_item_01 = (ImageView) findViewById(R.id.gallery_item_01);
		gallery_item_02 = (ImageView) findViewById(R.id.gallery_item_02);
		gallery_item_03 = (ImageView) findViewById(R.id.gallery_item_03);
		gallery_item_01.setOnClickListener(this);
		gallery_item_02.setOnClickListener(this);
		gallery_item_03.setOnClickListener(this);

		setupViews();
		init();
		pd = ProgressDialog.show(this, "", "数据加载中...", true, false);
		pd.setCancelable(true);
		new Thread(new Runnable() {

			@Override
			public void run() {

				lst = Parse.parseschool(1, 7);

				for (SchoolNew sp : lst) {
					String s = "";

					Map<String, Object> map = new HashMap<String, Object>();
					map.put("school_title", sp.getTitle());

					// s = NetWorkUtil.today(String.valueOf(sp.getTime()));
					map.put("school_instroduce", sp.getContent());
					ls.add(map);

				}

				final SchoolMienAdapter mienAdapter = new SchoolMienAdapter(
						SchoolMienActivity.this, ls);
				mHandler.post(new Runnable() {

					@Override
					public void run() {
						pd.dismiss();
						if (lst.size() > 0) {
							schoolmien_list.setAdapter(mienAdapter);

						} else {
							Toast.makeText(SchoolMienActivity.this,
									"获取数据失败...", Toast.LENGTH_SHORT).show();
						}

					}
				});

			}
		}).start();

		// 设置校园图片

		schoolmien_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Message msg = new Message();
				msg.what = 6;
				Bundle b = msg.getData();
				b.putString("url", lst.get(position).getContent());

				handler.sendMessage(msg);

			}
		});

	}

	private void setupViews() {

		mGalleryItem = (MyGallery) findViewById(R.id.schoolmien_gallery);
		for (int i = 0; i < 10; i++) {
			//----------------------------------
			
			
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inJustDecodeBounds = true;

			Bitmap bm = BitmapFactory.decodeResource(getResources(),
					R.drawable.image_1,options);
			float realWidth = options.outWidth;
			float realHeight = options.outHeight;
			int scale = (int) ((realHeight > realWidth ? realHeight : realWidth) / 100);
			if (scale <= 0) {
				scale = 1;
			}
			options.inSampleSize = scale;
			options.inJustDecodeBounds = false;
			bm = BitmapFactory.decodeResource(getResources(),
					R.drawable.image_1,options);

			
			//----------------------------------
//			Bitmap bt = BitmapFactory.decodeResource(getResources(),
//					R.drawable.image_1);

			lists.add(bm);

		}

		final ImageMienAdapter adapter = new ImageMienAdapter(this, lists);

		mGalleryItem.setAdapter(adapter);

		mGalleryItem.setSelection(0);

		mGalleryItem.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,

			int position, long id) {

				android.util.Log.d("setOnItemSelectedListener", "position: "
						+ position + "    id:"

						+ id);

				ImageView img = (ImageView) view;

				img.startAnimation(AnimationUtils.loadAnimation(

				SchoolMienActivity.this, R.anim.gallery_in));

			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {

				// TODO Auto-generated method stub

			}

		});

	}

	private void init() {

		final Timer timer = new Timer();

		timer.schedule(new TimerTask() {

			@Override
			public void run() {

				mHandler.sendEmptyMessage(timerAnimation);

			}

		}, 5000, 5000);

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {

		case R.id.gallery_item_01:
			gallery_item_01.setImageResource(R.drawable.gallery_item_select);
			gallery_item_02.setImageResource(R.drawable.gallery_item);
			gallery_item_03.setImageResource(R.drawable.gallery_item);
			mGalleryItem.setSelection(0);
			break;
		case R.id.gallery_item_02:
			gallery_item_01.setImageResource(R.drawable.gallery_item);
			gallery_item_02.setImageResource(R.drawable.gallery_item_select);
			gallery_item_03.setImageResource(R.drawable.gallery_item);
			mGalleryItem.setSelection(lists.size() / 2);
			break;
		case R.id.gallery_item_03:
			gallery_item_01.setImageResource(R.drawable.gallery_item);
			gallery_item_02.setImageResource(R.drawable.gallery_item);
			gallery_item_03.setImageResource(R.drawable.gallery_item_select);
			mGalleryItem.setSelection(lists.size() - 1);
			break;

		}

	}

}
